﻿using System;
using System.Collections.Generic;

using static Sgs.Utils.Method;

namespace Sgs.Model
{
    class Store
    {
        private readonly int TotalCardLength;
        private List<Card> Cards = null;
        private List<Card> Discards = new List<Card>();

        public Store(List<Card> Cards = null)
        {
            if(this.Cards == null)
            {
                this.Cards = Cards;
                TotalCardLength = Cards.Count;
            } else
            {
                throw new NullReferenceException("字段Store.Cards已经被初始化，无法再次赋值");
            }
        }

        public override string ToString()
        {
            return $"此时：总卡牌数={TotalCardLength} 当前牌堆数={Cards.Count} 当前弃牌堆数={Discards.Count}";
        }

        public void ThrowOneCard(Card card)
        {
            Discards.Add(card);
        }
        public List<Card> DispatchCards(int CardNumber = 0)
        {
            List<Card> Result = new List<Card>();
            if (Cards == null)
            {
                throw new NullReferenceException("字段Store.Cards为空，无法进行当前操作");
            }
            else if(CardNumber <= 0)
            {
                return Result;
            }

            else if(CardNumber > Cards.Count+ Discards.Count)
            {
                Console.WriteLine("牌堆里面已经没有牌了，游戏结束，即将开始平局结算");
                Result.Add(new Card("平局令牌", -1, Card.CardColor.方块, Card.CardPoint.A, Card.CardType.基本));
                return Result;
            }

            else if(CardNumber > Cards.Count)
            {
                Cards.AddRange(Discards);
                Cards = Shuffle(Cards);
                Discards.Clear();
                return DispatchCards(CardNumber);
            }

            for(int i  = 0; i< CardNumber; i++)
            {
                Result.Add(Cards[i]);
            }
            for (int i = 0; i < CardNumber; i++)
            {
                Cards.RemoveAt(i);
            }
            return Result;
        }
    }
}
